<?php


namespace app\adminapi\controller;

use think\facade\Db;
use think\facade\Request;

class Member extends Common
{
    public function list()
    {
        $data = $this->_vali([
            'keyword.default' => '',
            'doctimeStart.default' => '',
            'doctimeEnd.default' => '',
            'size.default' => '10',
        ], "POST");

        $where = [];

        if ($data['keyword']) {
            $where[] = ['name|phone|company_name', 'like', "%" . $data['keyword'] . "%"];
        }

        $list = Db::name('member')->where($where)->paginate($data['size']);

        $list = $list->toArray();

        foreach ($list['data'] as $key => $value) {
            if ($value['sex'] == 1) {
                $list['data'][$key]['sex'] = '男';
            } else {
                $list['data'][$key]['sex'] = '女';
            }
            
        }

        return json(['status' => 200, 'sum' => $list['total'], 'list' => $list['data']]);
    }

    /**
     * 添加/编辑
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save()
    {
        $data = Request::param();

        unset($data[0]);

        if ($data['work_time']) {
            $data['work_time'] = strtotime($data['work_time']);
        }
        if ($data['register']) {
            $data['register'] = strtotime($data['register']);
        }
        if (isset($data['party_time'])) {
            $data['party_time'] = strtotime($data['party_time']);
        }
        if ($data['birthday']) {
            $data['birthday'] = strtotime($data['birthday']);
        }

        try {
            if (isset($data['id'])) {
                $data['update_time'] = time();
                Db::name('member')->where('id', $data['id'])->update($data);
            } else {
                $data['create_time'] = time();
                $data['update_time'] = time();

                //var_dump($data);
                // exit();

                Db::name('member')->insert($data);
            }
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
        return json(['code' => 200, 'msg' => '操作成功']);
    }

    /**
     * 获取详情
     * @return \think\response\Json
     */
    public function get()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
        ], "POST");

        $data = Db::name('member')->where('id', $data['id'])->find();


        $data['sex'] = (string) $data['sex'];
        if ($data['birthday']) {
            $data['birthday'] = date('Y-m-d',$data['birthday']);
        }
        if ($data['party_time']) {
            $data['party_time'] = date('Y-m-d',$data['party_time']);
        }
        if ($data['register']) {
            $data['register'] = date('Y-m-d',$data['register']);
        }
        if ($data['work_time']) {
            $data['work_time'] = date('Y-m-d',$data['work_time']);
        }

        return json(['code' => 200, 'data' => $data]);
    }

    /**
     * 设置状态
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function status()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'status.require' => '请输入商户简称',
        ], "POST");

        $res = Db::name('role')->where('id', $data['id'])->update($data);
        if ($res) {
            return json(['code' => 200, 'msg' => '操作成功']);
        } else {
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del()
    {
        $data = $this->_vali([
            'ids.require' => 'ids不能为空',
        ], "POST");

        if (Db::name('role')->where('id', 'in', $data['ids'])->delete()) {
            return json(['code' => 200, 'data' => '操作成功']);
        } else {
            return json(['code' => 200, 'data' => '操作失败']);
        }
    }

    /**
     * 获取授权内容
     * @return \think\response\Json
     */
    public function author()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
        ], "POST");

        $list = Db::name('menu')->field('id,title as name,pid')->select();

        $map = $this->menu($list, 0);

        $checkedKeys = Db::name('role')->where('id', $data['id'])->value('rule_list');

        if ($checkedKeys) {
            $checkedKeys = explode(",", $checkedKeys);

            foreach ($checkedKeys as $key => $value) {
                $checkedKeys[$key] = (int)$value;
            }
        } else {
            $checkedKeys = [];
        }

        return json(['code' => 200, 'list' => $map, 'checkedKeys' => $checkedKeys]);
    }

    /**
     * 组装数据
     * @param $data
     * @param $pid
     * @return array
     */
    public function menu($data, $pid)
    {
        $ret = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $map['key'] = $v['id'];
                $map['title'] = $v['name'];
                if ($this->menu($data, $v['id'])) {
                    $map['children'] = $this->menu($data, $v['id']);
                }

                $ret[] = $map;
                unset($map);
            }
        }
        return $ret;
    }

    /**
     * 保存授权
     * @return \think\response\Json
     */
    public function save_rule()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'rule_list.require' => '请选择商户授权模块',
            'rule_list.array' => '商户授权模块必须是数组',
        ], "POST");

        $rule_list = implode(",", $data['rule_list']);

        if (Db::name('role')->where('id', $data['id'])->find()) {
            Db::name('role')->where('id', $data['id'])->update(['rule_list' => $rule_list]);
        } else {
            Db::name('role')->insert(['id' => $data['id'], 'rule_list' => $rule_list]);
        }
        return json(['code' => 200, 'msg' => '操作成功']);
    }

    public function report()
    {
        $data['count'] = Db::name('member')->count();
        $data['income_one'] = Db::name('member')->sum('income_one');
        $data['income_tow'] = Db::name('member')->sum('income_tow');

        return json(['status'=>200,'data'=>$data]);
    }

}